<?php

namespace App\Admin\Controllers;

use App\Http\Controllers\Controller;


use Encore\Admin\Controllers\HasResourceActions;
use Encore\Admin\Form;
use Encore\Admin\Grid;
use Encore\Admin\Layout\Content;
use Encore\Admin\Show;
use Encore\Admin\Facades\Admin;


use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;

use App\Logics\GoodsLogic;


class GoodsDoController extends BaseController
{
    use HasResourceActions;


    private function getInstance(){
        static $ob=null;
        if($ob===null){
            $ob=new GoodsLogic();
        }
        return $ob;
    }




    //商品软删除
    public function delete()
    {
        $goods_id = (int)request('goods_id');
        $ob=$this->getInstance();
        $data= $ob->delete($goods_id);
        $this->result($data);
    }

    //获取分类根据渠道
    public function getCategory()
    {
        $cha_id = (int)request('cha_id');
        $ob=$this->getInstance();
        $data=$ob->getCategory($cha_id);
        $this->result($data);
    }


    //处理商品批量审核，软删除，驳回，下架
    public function batchDeal()
    {
        $ids = request('ids');
        $type = request('type');
        $ob=$this->getInstance();
        $data=$ob->batchDeal($ids,$type);
        $this->result($data);
    }

    //上架
    public function shangjia()
    {
        $goods_ids = request('goods_ids');
        $chastr = request('chastr');
        $ob=$this->getInstance();
        $data =$ob->shangjia($goods_ids, $chastr);
        $this->result($data);
    }

    //删除，或不删除
    public function ajaxDel()
    {
        $goods_id = (int)request('goods_id');
        $ob=$this->getInstance();
        $data=$ob->ajaxDel($goods_id);
        $this->result($data);
    }

    //商品的渠道信息
    public function goodsChannelInfo()
    {
        $goods_id = (int)request('goods_id');
        $ob=$this->getInstance();
        $data= $ob->goodsChannelInfo($goods_id);
        $this->result($data);
    }

    //设置租期获取三个金额
    public function getLeaseMoneys(){
        $id=(int)request('id');
        $ob=$this->getInstance();
        $data= $ob->getLeaseMoneys($id);
        $this->result($data);

    }







    //删除商品图片
    public function goodsDeleteImg(Request $request)
    {
        if ($_POST) {
            $goods_id = (int)request('goods_id');
            $id = (int)request('id');
            $type = trim(request('type'));

            $ob= $this->getInstance();
            $data=$ob->goodsDeleteImg($request,$goods_id,$id,$type);
            $this->result($data);
        }
    }







    //生成租期html返回
    public function getZuqiHtml()
    {
        $term_num = (int)request('term_num');
        $rent_price = request('rent_price');
        $rent_price = (float)str_replace(',', '', $rent_price);

        if ($term_num < 1 || $term_num > 20) $term_num = 12;
        $rent_price = round($rent_price, 2);
        if ($rent_price < 0) $rent_price = 0;

        $price = number_deal($rent_price / $term_num);

        $html = "";
        for ($i = 1; $i <= $term_num; $i++) {
            $html .= '
                <div style="margin:5px;">
                    <div style="display:inline-block;text-align:center;width:60px;height:30px;background: #a1cbef;line-height: 30px;">第' . $i . '期</div>  本期租金：<input type="text" name="price[]" value="' . $price . '" style="margin-right:30px;">  天数: <input name="day_num[]"  value="30"  type="text" />
                </div>';
        }

        return json_success('ok', ['html' => $html]);
    }


    //规格参数删除
    public function goodsAttrDelete()
    {
        $id = (int)request('id');
        $data=GoodsLogic::goodsAttrDelete($id);
        $this->result($data);
    }

    //规格参数编辑
    public function goodsAttrEdit()
    {
        if($_POST){
            $id = (int)request('id');
            $name = trim(request('name'));
            $zhi = trim(request('zhi'));
            $para=[
                'id'=>$id,
                'name'=>$name,
                'zhi'=>$zhi
            ];
            $ob=$this->getInstance();
            $data=$ob->goodsAttrEdit($para);
            $this->result($data);
        }
    }

    public function attrHtml()
    {
        $html = '<div class="form-group  ">
            <label for="img" class="col-sm-2  control-label"></label>
            <div class="col-sm-8">
             参数名： <input type="text"  name="name[]" style="padding-left:5px;border:1px solid #a6e1ec;height:40px;width:300px;"  />
            参数值：<input type="text"  name="val[]"  style="padding-left:5px;border:1px solid #a6e1ec;height:40px;width:300px;"   />
            <span onclick="$(this).parent().parent().remove();">【 删除 】</span>
            </div >
            </div >';

        return json_success('ok', ['html' => $html]);
    }



    public function skuHtml()
    {
        $html = '<div style="padding:5px;"> <input style="padding-left:5px;height:30px;width:300px;border:1px solid #a1cbef" type="text" name="zhi[]" /><a onclick="del_zhi(this)">【-】</a></div>';

        return json_success('ok', ['html' => $html]);
    }

    //修改属性名
    public function ajaxEditAttrType()
    {
        if($_POST){
            $id = (int)request('id');
            $goods_id = (int)request('goods_id');
            $attr_type = trim(request('attr_type'));
            $para=[
                'id'=>$id,
                'goods_id'=>$goods_id,
                'attr_type'=>$attr_type
            ];

            $ob=$this->getInstance();
            $data=$ob->ajaxEditAttrType($para);
            $this->result($data);
        }
    }

    //删除属性 同时要删除对应的属性值，sku,租期
    public function ajaxDelAttrType()
    {
        $attr_id = (int)request('attr_id');

        $ob= $this->getInstance();
        $data=$ob->ajaxDelAttrType($attr_id);
        $this->result($data);
    }

    //修改 attr_val  销售属性值
    public function ajaxEditAttrVal()
    {
        $id = (int)request('id');
        $attr_id = (int)request('attr_id');
        $attr_name = trim(request('attr_name'));
        $para=[
            'id'=>$id,
            'attr_id'=>$attr_id,
            'attr_name'=>$attr_name
        ];
        $ob=$this->getInstance();
        $data=$ob->ajaxEditAttrVal($para);
        $this->result($data);
    }

    //删除属性值
    public function ajaxDelAttrVal()
    {
        $id = (int)request('id');
        $ob= $this->getInstance();
        $data=$ob->ajaxDelAttrVal($id);
        $this->result($data);
    }

    //属性图片上传处理
    public function ajaxUploadAttrImg(Request $request)
    {
        if ($_POST) {
            $id = (int)request('id');
            $ob=$this->getInstance();
            $data=$ob->ajaxUploadAttrImg($request,$id);
            $this->result($data);
        }
    }

    //删除属性值中的图片
    public function ajaxDelAttrImg()
    {
        $id = (int)request('id');
        $ob=$this->getInstance();
        $data=$ob->ajaxDelAttrImg($id);
        $this->result($data);
    }




    //给商品设置一组sku
    public function ajaxSetGoodsSku()
    {
        if ($_POST) {
            $goods_id = (int)request('goods_id');
            $sku = $_POST['sku'];
            $goods_price = trim(request('goods_price'));
            $goods_official_price = trim(request('goods_official_price'));
            $goods_purchase_price = trim(request('goods_purchase_price'));

            $goods_price = (float)str_replace(',', '', $goods_price);
            $goods_official_price = (float)str_replace(',', '', $goods_official_price);
            $goods_purchase_price = (float)str_replace(',', '', $goods_purchase_price);

            $para=[
                'goods_id'=>$goods_id,
                'sku'=>$sku,
                'goods_price'=>$goods_price,
                'goods_official_price'=>$goods_official_price,
                'goods_purchase_price'=>$goods_purchase_price
            ];
            $ob=$this->getInstance();
            $data=$ob->ajaxSetGoodsSku($para);
            $this->result($data);
        }
        die(json_error('非法请求'));
    }



    //删除一组sku,
    public function goodsSkuDelete()
    {
        if ($_POST) {
            $id = (int)request('id');
            $ob=$this->getInstance();
            $data = $ob->goodsSkuDelete($id);
            $this->result($data);
        }
    }



}